package org.streets.eis.component.codegroup;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.Strings;
import org.streets.commons.util.JDBCUtils;
import org.streets.database.Limits;
import org.streets.database.SQLConnection;
import org.streets.database.SQLNestedException;

import org.streets.eis.component.combobox.ListComboBox;
import org.streets.eis.component.model.ListItem;

public class ListCodeGroup extends ListComboBox {

	private static final long serialVersionUID = 1L;

	private String codeGroupId = "";
	private String conditionSql = "";

	@Inject
	private SQLConnection conn;
	
	public ListCodeGroup(String id, String codeGroupId){
		this(id,codeGroupId,true);
	}
	
	public ListCodeGroup(String id, String codeGroupId,String conditionSql){
		this(id,codeGroupId,conditionSql,true);
	}	
	
	public ListCodeGroup(String id, String codeGroupId,boolean canInput){
		this(id,codeGroupId,"",true);
	}
	
	public ListCodeGroup(String id, String codeGroupId,String conditionSql,boolean canInput){
		super(id,null,canInput);
		this.conditionSql = conditionSql;
		setCodeGroupId(codeGroupId);
	}		
	
	public ListCodeGroup(String id, IModel<ListItem> model,String codeGroupId){
		this(id,model,codeGroupId,true);
	}
	
	public ListCodeGroup(String id, IModel<ListItem> model,String codeGroupId,String conditionSql){
		this(id,model,codeGroupId,conditionSql,true);
	}
	
	public ListCodeGroup(String id, IModel<ListItem> model,String codeGroupId,boolean canInput){
		this(id,model,codeGroupId,"",canInput);
	}
	
	public ListCodeGroup(String id, IModel<ListItem> model,String codeGroupId,String conditionSql,boolean canInput){
		super(id,model,null,canInput);
		this.conditionSql = conditionSql;
		setCodeGroupId(codeGroupId);
	}

	public String getCodeGroupId() {
		return codeGroupId;
	}

	public void setCodeGroupId(String codeGroupId) {
		this.codeGroupId = codeGroupId;
		setDataModel(generateCodeGroupDataModel());
	}
	
	public String getConditionSql() {
		return conditionSql;
	}

	public void setConditionSql(String conditionSql) {
		this.conditionSql = conditionSql;
		setDataModel(generateCodeGroupDataModel());
	}	
	
	protected List<ListItem> generateCodeGroupDataModel(){
		if (Strings.isEmpty(codeGroupId)){
			return null;
		} 
		
		String sql =  String.format("select id,name from eis_code_item a  where group_id='%s' %s order by code",
				codeGroupId,Strings.isEmpty(conditionSql)?"":" and " +conditionSql );		
		ResultSet rs = conn.query(sql, Limits.none());
		List<ListItem> model = new ArrayList<ListItem>();
		try{
			while (rs.next()){
				model.add(new ListItem(rs.getString(1),rs.getString(2)));
			}
		} catch (Exception ex) {
			throw new SQLNestedException(sql, ex);
		} finally {
			JDBCUtils.close(rs);
		}
		return model.size()>0?model:null;
	}
	
	@Override
	protected Iterator<ListItem> getMatchedItems(String key) {
		if (Strings.isEmpty(codeGroupId)){
			return new ArrayList<ListItem>().iterator();
		} 
		
		String sql = String.format("select id,name from eis_code_item a  where group_id='%s' %s and (%s or %s) order by code",
				codeGroupId,Strings.isEmpty(conditionSql)?"":" and " +conditionSql,generateLikeSql("name",key),generateLikeSql("alias",key));  
		ResultSet rs = conn.query(sql, Limits.of(0, 10));
		List<ListItem> matcheds = new ArrayList<ListItem>();
		try{
			while (rs.next()){
				matcheds.add(new ListItem(rs.getString(1),rs.getString(2)));
			}
		} catch (Exception ex) {
			throw new SQLNestedException(sql, ex);
		} finally {
			JDBCUtils.close(rs);
		}
		return matcheds.iterator();	
	}
	
	@Override
	protected void onItemChanged(ListItem newValue){
		onItemChanged(this,newValue);
	}

	@Override
	protected void onAjaxItemChanged(ListItem newValue,AjaxRequestTarget target){
		onAjaxItemChanged(this,newValue,target);
	}		
	
	protected void onItemChanged(ListCodeGroup codeGroup,ListItem newValue){		
	}
	
	protected void onAjaxItemChanged(ListCodeGroup codeGroup,ListItem newValue,AjaxRequestTarget target){	
	}
	
}
